package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.PartnerMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.PartnerAddDto;
import com.heima.pojo.entity.Partner;
import com.heima.pojo.entity.VendingMachine;
import com.heima.pojo.vo.PageResponseVo;
import com.heima.pojo.vo.PartnerVo;
import com.heima.service.PartnerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements PartnerService {


    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private PartnerMapper partnerMapper;

    @Override
    public PageResponseVo<PartnerVo> searchPage(Integer pageSize, Integer pageIndex, String name) {
        //1.分页查询合作商数据
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        if (name != null){
            wrapper.like(Partner::getName, name);
        }
        Page<Partner> partnerPage = this.page(new Page<>( pageIndex ,pageSize), wrapper);
        //遍历数据复制给PartnerVo
        List<PartnerVo> voList = new ArrayList<>();
        for (Partner partner : partnerPage.getRecords()) {
            PartnerVo partnerVo = new PartnerVo();
            BeanUtils.copyProperties(partner, partnerVo);
            LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VendingMachine::getOwnerName, partner.getName());
            Integer result = vendingMachineMapper.selectCount(queryWrapper);
            partnerVo.setVmCount(result);
            voList.add(partnerVo);
        }
        PageResponseVo<PartnerVo> response = new PageResponseVo<>();
        response.setTotalCount(partnerPage.getTotal());
        response.setPageSize(pageSize);
        response.setPageIndex(pageIndex);
        response.setCurrentPageRecords(voList);
        response.setTotalPage(partnerPage.getPages());
        return response;
    }

    @Override
    public boolean addPartner(PartnerAddDto partnerAddDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerAddDto, partner);
        return  partnerMapper.insert(partner) > 0 ;
    }
    @Override
    public PartnerVo getPartner(Long id) {
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getId, id);
        Partner partner = this.getOne(wrapper);
        PartnerVo partnerVo = new PartnerVo();
        LambdaQueryWrapper<VendingMachine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VendingMachine::getOwnerName, partner.getName());
        partnerVo.setVmCount(vendingMachineMapper.selectCount(queryWrapper));
        BeanUtils.copyProperties(partner, partnerVo);
        return partnerVo;
    }

    @Override
    public boolean updatePartner(Long id, PartnerAddDto partnerAddDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerAddDto, partner);
        LambdaQueryWrapper<Partner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Partner::getId, id);
        return partnerMapper.update(partner, wrapper) > 0;
    }
}
